package com.cuanbobox.utils;

import android.text.TextUtils;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * 互动课堂指令发送
 */
public class TcpUtil {

    /**
     * Tcp指令发送
     *
     * @param ip ip地址
     * @param port 端口号
     * @param cmd 要发送的指令
     */
    public static void send(final String ip, final int port, final String cmd) {
        if (TextUtils.isEmpty(ip) || TextUtils.isEmpty(cmd)) return;
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    LogUtil.add("ip",ip,"TcpUtil");
                    LogUtil.add("port",""+port,"TcpUtil");
                    LogUtil.add("cmd",cmd,"TcpUtil");
                    Socket socket = new Socket(ip, port);
                    if (socket.isConnected()) {
                        LogUtil.add("状态","连接成功","TcpUtil");
                        OutputStream output = socket.getOutputStream();
                        output.write(cmd.getBytes("utf-8"));
                        output.flush();
                        output.close();
                        socket.close();
                    }else {
                        LogUtil.add("状态","连接失败","TcpUtil");
                    }
                } catch (Exception e) {
                    LogUtil.addError("==========================TcpUtil========================");
                    LogUtil.addError("ip="+ip);
                    LogUtil.addError("port="+port);
                    LogUtil.addError("cmd="+cmd);
                    LogUtil.addError(e);
                    LogUtil.addError("==================================================");
                }
            }
        }).start();
    }

    /**
     * Tcp指令发送
     *
     * @param ip ip地址
     * @param port 端口号
     * @param bytes 要发送的字节数组
     */
    public static void send(final String ip, final int port, final byte[] bytes) {
        if (TextUtils.isEmpty(ip) || bytes == null) return;
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Socket socket = new Socket(ip, port);
                    if (socket.isConnected()) {
                        OutputStream output = socket.getOutputStream();
                        output.write(bytes);
                        output.flush();
                        output.close();
                        socket.close();
                        String content = "";
                        for (int i = 0; i < bytes.length; i++) {
                            content += "bytes[" + i + "]=" + bytes[i] + " hex=" + String.format("%02X", bytes[i]) + " ";
                        }
                        LogUtil.add("数据发送成功", content, "TcpUtil");
                    }
                } catch (Exception e) {
                   LogUtil.addError(e);
                }
            }
        }).start();
    }

    /**
     *发送指令到服务器（端口号为9191）
     * @param ip ip地址
     * @param cmd 指令
     */
    public static String sendToCmdServer(final String ip, final String cmd) {
        String result = "";
        try {
            Socket socket = new Socket(ip, 9191);
            socket.setSoTimeout(3000);
            if (socket.isConnected()) {
                OutputStream output = socket.getOutputStream();
                output.write(cmd.getBytes("utf-8"));
                output.flush();
                InputStream input = socket.getInputStream();
                byte buffer[] = new byte[1024];
                int count = input.read(buffer);
                if (count != -1) {
                    result = new String(buffer, 0, count, "utf-8");
                }
                output.close();
                input.close();
                socket.close();
            }
        } catch (Exception e) {
            LogUtil.addError("-----------TcpUtil异常----------");
            LogUtil.addError(e);
            LogUtil.addError("--------------------------------");
        }
        return result;
    }
}
